package cn.jtfadmin.cms.controller;

import cn.jtfadmin.base.dict.dto.DictItemModelDTO;
import cn.jtfadmin.base.lang.common.domain.dto.PageRDTO;
import cn.jtfadmin.base.lang.common.domain.dto.ResultDTO;
import cn.jtfadmin.base.lang.common.domain.dto.SpecPDTO;
import cn.jtfadmin.base.lang.common.domain.dto.SpecPagePDTO;
import cn.jtfadmin.cms.domain.dto.CmsAddPDTO;
import cn.jtfadmin.cms.domain.dto.CmsEditPDTO;
import cn.jtfadmin.cms.domain.dto.CmsModelDTO;
import cn.jtfadmin.cms.domain.entity.CmsEntity;
import cn.jtfadmin.cms.service.CmsService;
import cn.jtfadmin.base.lang.config.excel.ExcelWeb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * cms 后台controller
 * @author jtf
 * @since 0.0.2
 */
@RestController
public class CmsController {

    @Autowired
    private CmsService service;


    /**
     * 分页查询
     *
     * @param specPagePDTO 查询条件
     * @return 分页数据
     */
    @GetMapping(value = "/cms/info/list")
    public ResultDTO<PageRDTO<CmsModelDTO>> list(SpecPagePDTO<CmsEntity> specPagePDTO) {
        return ResultDTO.successData(service.findModelAll(specPagePDTO,CmsModelDTO.class));
    }

    /**
     * 字典
     *
     * @param columns 命名列
     * @return 无
     */
    @GetMapping(value = "/cms/info/named")
    public ResultDTO<List<DictItemModelDTO>> named(@RequestParam(required = false) String columns,
                                                   @NotNull SpecPDTO<CmsEntity> specPDTO,
                                                   @RequestParam(required = false) String keyFields) {
        return ResultDTO.successData(service.findDictModelListBySpec(columns, specPDTO, keyFields));
    }

    /**
     * 新增
     *
     * @param addPDTO 新增参数
     * @return id
     */
    @PostMapping(value = "/cms/info/add")
    public ResultDTO<String> add(@RequestBody @Validated CmsAddPDTO addPDTO) {
        return ResultDTO.successData(service.add(addPDTO).getId());
    }

    /**
     * 编辑
     *
     * @param editPDTO 查询条件
     * @return 无
     */
    @PutMapping(value = "/cms/info/edit")
    public ResultDTO<String> edit(@RequestBody @Validated CmsEditPDTO editPDTO) {
        return ResultDTO.successData(service.edit(editPDTO).getId());
    }

    /**
     * 按照id删除
     *
     * @param id id
     * @return 无
     */
    @DeleteMapping(value = "/cms/info/del")
    public ResultDTO<String> del(@RequestParam() @NotBlank String id) {
        service.deleteById(id);
        return ResultDTO.success();
    }

    /**
     * 批量删除
     *
     * @param ids 查询条件
     * @return 分页数据
     */
    @DeleteMapping(value = "/cms/info/delBatch")
    public ResultDTO<String> delBatch(@RequestParam() @NotBlank String ids) {
        service.deleteBatch(ids);
        return ResultDTO.success();
    }

}
